﻿using UnityEngine;
using System.Collections;

public class Seeker : MonoBehaviour {
    private bool touching = false;
    private float sensivity = 0.1f;
    private Vector2 touchBegin = new Vector2(0, 0);
    private Vector2 direction = new Vector2(0, 0);

    private float zPosition = 0;

    private bool onlyOnce = true;

    // Use this for initialization
    void Start()
    {

	
	}
	
	// Update is called once per frame
    void Update()
    {
        if (GameState.Instance.GetState().Equals(State.SeekingPhase) && onlyOnce)
        {
            zPosition = transform.position.z;

            Vector3 transformTemp = transform.position;

            transformTemp.x = DataStorage.Instance.GetRoomList()[0].GetCenter().x;
            transformTemp.y = DataStorage.Instance.GetRoomList()[0].GetCenter().y;

            transform.position = transformTemp;

            onlyOnce = false;
        }

        Move();
    }

    private void Move()
    {
        if (GameState.Instance.GetState().Equals(State.SeekingPhase))
        {
#if UNITY_ANDROID && !UNITY_EDITOR
        if (touching)
            this.GetComponent<CharacterController>().Move(new Vector3(direction.x, direction.y, 0) * sensivity);

        if (Input.touchCount > 0)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                Debug.Log("Touch began at: " + Input.touches[0].position.y);
                touchBegin = Input.touches[0].position;
                touching = true;
            }
            if (Input.touches[0].phase == TouchPhase.Moved)
            {
                direction = Input.touches[0].position - touchBegin;
                float vectorLength = Mathf.Sqrt(direction.x * direction.x + direction.y * direction.y);
                direction = direction / vectorLength;
            }
            if (Input.touches[0].phase == TouchPhase.Ended)
                touching = false;
        }
#else
            this.GetComponent<CharacterController>().Move(new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0));
#endif
        }
    }
}
